package com.shujia.flink.transform;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Demo03Filter {
    public static void main(String[] args) throws Exception {
        /*
         * filter：用于过滤，根据方法返回的布尔值进行过滤
         * 返回true则保留数据
         * 返回false则过滤数据
         */

        // 过滤出文科班的女生
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();


        // 提取students中的name
        DataStream<String> studentDS = env.readTextFile("flink/data/students.txt");

        DataStream<String> clazz = studentDS
                .filter(new FilterFunction<String>() {
                    @Override
                    public boolean filter(String value) throws Exception {
                        // 分别提取班级以及性别
                        String[] splits = value.split(",");
                        String clazz = splits[4];
                        String gender = splits[3];

//                        System.out.println(clazz);
//                        System.out.println(gender);

                        return clazz.startsWith("文科") && "女".equals(gender);
                    }
                });

        /*
         * FLink中的算子不是懒执行的，不需要action算子触发
         * 统一由env.execute()触发Flink任务的执行
         */
//        clazz.print();

        env.execute();


    }
}
